﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class UserSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public UserSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
        }

        public USERS GetByUserName(string userName)
        {
            try
            {
                return dbContext.USERS.Where(p => p.USERNAME.ToUpper() == userName.ToUpper()).First();
            }
            catch { return null; }
        }

        public List<USERS> GetList()
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;

                return dbContext.USERS.ToList();

            }
            catch { return null; }
        }

        public List<USERS> GetUserListOnDON(string mDV, string tokt)
        {
            try
            {
                var c = (from i in dbContext.USERS
                         join k in dbContext.USER_DONVI on i.USERID equals k.USERID
                         where ( k.DONVIID == mDV || string.IsNullOrEmpty(mDV)) &&
                               (k.TOID == tokt || string.IsNullOrEmpty(tokt))
                         select k.USERID.Value).ToArray();

                
                var UserArray = c.Distinct().ToArray<decimal>();

                return dbContext.USERS.Where(p => UserArray.Contains(p.USERID)).ToList();

            }
            catch
            {

                List<USERS> _lr = new List<USERS>();
                return _lr;
            }
        }

        

        public bool DeleteByUserId(int id)
        {
            try
            {
                var user = GetByUserById(id);
                dbContext.USERS.Remove(user);
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }

        public USERS GetByUserByEmail(string Email)
        {
            try
            {
                return dbContext.USERS.Where(p => p.USERNAME == Email).First();
            }
            catch { return null; }
        }

        public USERS GetByUserById(int id)
        {
            try
            {
                return dbContext.USERS.Where(p => p.USERID == id).First();
            }
            catch { return null; }
        }



        public decimal addOrupdate(USERS entity)
        {
            if (entity.USERID == 0) dbContext.USERS.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.USERID;
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            try
            {
                var entityUser = dbContext.USERS.First(k => k.USERNAME == username && k.PASSWORD == oldPassword);
                entityUser.PASSWORD = newPassword;
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }

        public bool deleteByName(string Name)
        {
            try
            {
                var entity = GetByUserName(Name);
                var listUserOnRoles = dbContext.USER_ROLES.Where(p => p.USERID == entity.USERID).ToList();
                if (listUserOnRoles != null && listUserOnRoles.Count() > 0)
                {
                    foreach (var item in listUserOnRoles)
                    {
                        dbContext.USER_ROLES.Remove(item);
                        dbContext.SaveChanges();
                    }
                }
                dbContext.USERS.Remove(entity);
                dbContext.SaveChanges();

                return true;
            }
            catch { return false; }
        }


        public bool ValidateUser(string username, string password)
        {
            try
            {
                var entityUser = dbContext.USERS.SingleOrDefault(k => k.USERNAME == username && k.PASSWORD == password);
                if (entityUser != null)
                    return true;
                else return false;
            }
            catch { return true; }
        }

        public USERS LogIn(string username, string Password)
        {
            try
            {
                username = username.ToUpper();
                var entityUser = dbContext.USERS.SingleOrDefault(k => k.USERNAME.ToUpper() == username && k.PASSWORD == Password);
                if (entityUser != null)
                    return entityUser;
                else return null;
            }
            catch { return null; }
        }

        public string[] GetRolesForUser(string username)
        {
            try
            {
                var c = (from i in dbContext.USERS
                         join k in dbContext.USER_ROLES on i.USERID equals k.USERID
                         join l in dbContext.ROLES on k.ROLEID equals l.ROLEID
                         where i.USERNAME == username
                         select l.TEN).ToArray();
                return c;
            }
            catch { return new string[0]; }
        }

        public decimal[] GetIdRolesForUser(string username)
        {
            try
            {
                var c = (from i in dbContext.USERS
                         join k in dbContext.USER_ROLES on i.USERID equals k.USERID
                         join l in dbContext.ROLES on k.ROLEID equals l.ROLEID
                         where i.USERNAME == username
                         group l by l.ROLEID into h
                         select h.Key).ToArray();
                return c;
            }
            catch { return new decimal[0]; }
        }

        public bool checkExitsRolesiD(decimal userId, decimal RoledID)
        {
            try
            {
                var c = dbContext.USER_ROLES.SingleOrDefault(k => k.USERID == userId && k.ROLEID == RoledID);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public void removeRolesInUsser(decimal userId, decimal RoledID)
        {
            try
            {
                var c = dbContext.USER_ROLES.SingleOrDefault(k => k.USERID == userId && k.ROLEID == RoledID);
                dbContext.USER_ROLES.Remove(c);
                dbContext.SaveChanges();
            }
            catch { }
        }




        public bool checkExitsLoaiSiteId(decimal userId, string LoaiSiteId)
        {
            try
            {
                var c = dbContext.USER_SITE.SingleOrDefault(k => k.USERID == userId && k.LOAISITE == LoaiSiteId);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public void removeLoaiSiteInUsser(decimal userId, string LoaiSiteId)
        {
            try
            {
                var c = dbContext.USER_SITE.SingleOrDefault(k => k.USERID == userId && k.LOAISITE == LoaiSiteId);
                dbContext.USER_SITE.Remove(c);
                dbContext.SaveChanges();
            }
            catch { }
        }

        public bool checkExitsQuyenHT(decimal userId, decimal quyenHTID)
        {
            try
            {
                var c = dbContext.USER_QUYENHT.SingleOrDefault(k => k.USERID == userId && k.QUYENHTID == quyenHTID);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public void removeUserQuyenHTInUsser(decimal userId, decimal quyenHTID)
        {
            try
            {
                var c = dbContext.USER_QUYENHT.SingleOrDefault(k => k.USERID == userId && k.QUYENHTID == quyenHTID);
                dbContext.USER_QUYENHT.Remove(c);
                dbContext.SaveChanges();
            }
            catch {  }
        }


        public bool checkExitsDomvi(decimal userId, string donviId)
        {
            try
            {
                var c = dbContext.USER_DONVI.SingleOrDefault(k => k.USERID == userId && k.DONVIID == donviId);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public void removeUserDonvi(decimal userId, string donviId)
        {
            try
            {
                var c = dbContext.USER_DONVI.SingleOrDefault(k => k.USERID == userId && k.DONVIID == donviId);
                dbContext.USER_DONVI.Remove(c);
                dbContext.SaveChanges();
            }
            catch { }
        }

        public bool checkExitsToKT(decimal userId, string toId)
        {
            try
            {
                var c = dbContext.USER_DONVI.SingleOrDefault(k => k.USERID == userId && k.TOID == toId);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public void removeUserToKT(decimal userId, string toId)
        {
            try
            {
                var c = dbContext.USER_DONVI.SingleOrDefault(k => k.USERID == userId && k.TOID == toId);
                dbContext.USER_DONVI.Remove(c);
                dbContext.SaveChanges();
            }
            catch { }
        }

        public string[] ListDonViByUser(int UserId)
        {
            try
            {
                List<string> _listDonvi = new List<string>();
                var c = (from i in dbContext.USER_DONVI
                         where i.USERID == UserId
                         group i by i.DONVIID into h
                         select h.Key).ToList();

                foreach (var item in c)
                {
                    _listDonvi.Add(item + "-DV");
                }

                return _listDonvi.ToArray();
            }
            catch { return null; }
        }

        public string[] ListToKTByUser(decimal UserId)
        {
            try
            {
                List<string> _listTOKT = new List<string>();

                var c = (from i in dbContext.USER_DONVI
                         where i.USERID == UserId
                         group i by i.TOID into h
                         select h.Key).ToList();
                foreach (var item in c)
                {
                    _listTOKT.Add(item + "-TO");
                }

                return _listTOKT.ToArray();
            }
            catch { return null; }
        }

        public string[] ListToKTByUserMain(decimal UserId)
        {
            try
            {
                List<string> _listTOKT = new List<string>();

                var c = (from i in dbContext.USER_DONVI
                         where i.USERID == UserId
                         group i by i.TOID into h
                         select h.Key).ToList();
                foreach (var item in c)
                {
                    _listTOKT.Add(item);
                }

                return _listTOKT.ToArray();
            }
            catch { return null; }
        }

    }
}
